__author__ = 'st316'
'''
Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.
O(N)
'''


class Solution:
    # @return an integer
    def romanToInt(self, s):
        mapper = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
        n = mapper[s[-1]]
        for x in xrange(len(s) - 2, -1, -1):
            if mapper[s[x]] < mapper[s[x + 1]]:
                n -= mapper[s[x]]
            else:
                n += mapper[s[x]]
        return n


if __name__ == '__main__':
    s = Solution()
    print s.romanToInt('VIV')